//
//  WDJNetHelper.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJNetHelper.h"
@interface WDJNetHelper()
@property (strong, nonatomic) AFHTTPSessionManager *netManager;
@end
@implementation WDJNetHelper
static WDJNetHelper *defaultManager = nil;
 + (WDJNetHelper*)defaultRequest{
     static dispatch_once_t token;
     dispatch_once(&token, ^{
         if(defaultManager == nil){
             defaultManager = [[WDJNetHelper alloc] init];
         }
     });
     return defaultManager;
}
//网络类初始化
- (instancetype)init{
  if(self = [super init]){
      self.netManager = [AFHTTPSessionManager manager];
      self.netManager.requestSerializer = [AFHTTPRequestSerializer serializer];
      self.netManager.responseSerializer = [AFHTTPResponseSerializer serializer];
      self.netManager.requestSerializer.timeoutInterval = 10.0;
  }
  return self;
}
- (void)netPost:(NSString *)requestUrl param:(NSDictionary *)paramDic success:(void(^)(id data))sucBlack fail:(void(^)(void))failBlack{
    [self.netManager POST:requestUrl parameters:paramDic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(responseObject){
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            if([jsonDict[@"code"] intValue] == 200){
                sucBlack(jsonDict);
            }else{
                [SVProgressHUD showWithStatus:jsonDict[@"msg"]];
                failBlack();
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlack();
    }];
}
@end
